<!-- 通知分类 -->
<template>
  <div class="noticeCategory sys-content">
    <page-mod>
      <el-card>
        <div class="query-box">
          <el-form :model="query" inline>
            <el-form-item label="分类名称:">
              <el-input v-model="query.name" placeholder="请输入分类名称" clearable/>
            </el-form-item>
          </el-form>
          <div class="btns">
            <el-button icon="Search" color="#16b777" @click="load(1)">查询</el-button>
            <el-button icon="Plus" color="#FFB800" @click="show = true">新增</el-button>
            <el-button icon="Refresh" color="#FF5722" @click="resetSearch">重置</el-button>
          </div>
        </div>
      </el-card>
    </page-mod>
    <page-mod title="通知分类数据" icon="Histogram" :showBtn="false" :padBottom="false">
      <el-card v-loading="loading" element-loading-text="拼命加载中">
        <el-table :data="dataList" max-height="493" border>
          <el-table-column label="序号" type="index" align="center" width="60"/>
          <el-table-column label="分类名称" prop="name" align="center"></el-table-column>
          <el-table-column label="描述" prop="desc" header-align="center"></el-table-column>
          <el-table-column label="创建时间" prop="createdTime" align="center">
            <template #default="{row}">
              <span>{{ $date(row.createdTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="180">
            <template #default="{row}">
              <el-button type="primary" icon="Edit" link @click="toUpdate(row)">修改</el-button>
              <el-divider direction="vertical" />
              <el-button type="danger" icon="Delete" link @click="doDelete(row.roleId)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination 
          class="paging"
          v-show="query.total"
          small
          layout="total, sizes, prev, pager, next, jumper" 
          :current-page="query.current" 
          :page-size="query.size"
          :total="query.total"
          @current-change="load"
          @size-change="sizeChange">
        </el-pagination>
      </el-card>
    </page-mod>

    <!-- 新增、编辑弹窗 -->
    <el-dialog v-model="show" :title="form.id ? '编辑' : '新增'" top="10vh" width="23%" center draggable @close="resetForm">
      <el-form ref="formRef" :model="form" :rules="rules" size="default" label-width="85px">
        <el-form-item label="分类名称:" prop="name" required>
          <el-input v-model="form.name" placeholder="请输入分类名称" clearable/>
        </el-form-item>
        <el-form-item label="描述:" prop="desc">
          <el-input v-model="form.desc" type="textarea" rows="5" placeholder="请输入描述" clearable/>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" size="default" @click="doSubmit">保存</el-button>
        <el-button size="default" @click="show = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="noticeCategory">
  import { ref, getCurrentInstance, onActivated } from 'vue'

  // 获取注册的全局实列
  const currentThis = getCurrentInstance().appContext.config.globalProperties
  const formRef = ref(null)

  let loading = ref(false)
  let show = ref(false)
  let query = ref({
    name: '',
    current: 1,
    size: 20,
    total: 1
  })
  let form = ref({
    id: '',
    name: '',
    desc: ''
  })
  let rules = ref({
    name: [{ required: true, message: '请输入分类名称', trigger: ['blur', 'change'] }]
  })
  let dataList = ref([
    {id: 1, name: '士大夫但是', desc: '士大夫但是发放', createdTime: '2023-05-12 13:25:56'}
  ])

  function load(page) {
    // currentData.loading = true
    // currentData.query.current = page
    // getRolePagesApi(currentData.query).then(res => {
    //   currentData.loading = false
    //   if(res.code === '200') {
    //     currentData.dataList = res.data.pages
    //     currentData.query.total = res.data.total
    //   } else {
    //     currentThis.$message.error(res.message)
    //   }
    // })
  }

  // 新增或编辑
  function doSubmit() {
    formRef.value.validate((valid) => {
      if (valid) {
        currentThis.$messageBox.confirm(`确认${form.value.id ? '修改' : '新增'}吗？`).then(() => {
          let loading = currentThis.$loading(`正在${form.value.id ? '修改' : '新增'}`)
          saveOrUpdateRoleApi(`${form.value.id ? 'update' : 'save'}`, form.value).then(res => { // 修改
            loading.close()
            if(res.code === '200') {
              load(form.value.id ? query.value.current : 1)
              show.value = false
              currentThis.$message.success(res.message)
            } else {
              currentThis.$message.error(res.message)
            }
          })
        }).catch(() => {})
      } else {
        return false;
      }
    })
  }

  function toUpdate(e) {
    form.value = {...e}
    show.value = true
  }

  function doDelete(id) {
    currentThis.$messageBox.confirm('确认删除吗？').then(() => {
      let loading = currentThis.$loading('删除中，请耐心等待')
      deleteRoleApi({ id: id }).then(res => {
        loading.close()
        if(res.code === '200') {
          currentThis.$message.success(res.message)
          load(currentData.query.current)
        } else {
          currentThis.$message.error(res.message)
        }
      })
    }).catch(() => {})
  }

  // 新增编辑弹窗重置数据
  function resetForm() {
    form.value = {
      id: '',
      name: '',
      desc: ''
    }
    formRef.value.resetFields()
  }

  // 重置查询
  function resetSearch() {
    query.value.name = ''
    load(1)
  }

  // 分页每页显示条数改变执行
  function sizeChange(size) {
    query.value.size = size
    load(1)
  }

  onActivated(() => {
    load(query.value.current)
  })
</script>

<style lang="less" scoped>
</style>